(function($) {
    $(function(event) {

        var city = $('select#selector-city-upd');

        var userType = $('select#user-type');

        userType.change(function() {
            showBlock();
        });

        city.change(function() {
            if (city.val() == -1) {
                showManualCity();
            } else {
                hideManualCity();
            }
        });

        function showBlock() {
            if (city.val() == -1) {
                showManualCity();
            } else {
                hideManualCity();
            }

            hideForUser();
            hideForAgency();
            hideForOperator();
            hideForCorporation();
            hideForUserManager();
            hideForAgencyManager();
            hideForCorporationManager();

            if(userType.val()) {
                switch(userType.val()) {
                    /** no user type */
                    case '0':
                    break;
                    /** user */
                    case '1':
                        showForUser();
                        showForUserManager();
                    break;
                    /** agency */
                    case '2':
                        showForAgency();
                    break;
                    /** operator */
                    case '64':
                        showForAgency();
                        showForOperator();
                    break;
                    /** manager */
                    case '4':
                        showForUser();
                    break;
                    /** admin */
                    case '8':
                        showForUser();
                    break;
                    case '32':
                        showForCorporation();
                        showForCorporationManager();
                    break;
                }
            }
        }

        function showManualCity() {
            $('dl#show-edit-no-city').show();
            $('input#no-city').rules('add', {required: true});
        }

        function hideManualCity() {
            $('dl#show-edit-no-city').hide();
            $('input#no-city').rules('add', {required: false});
        }

        function showForUser() {
            $('dl.user').show();
        }

        function hideForUser() {
            $('dl.user').hide();
        }

        function showForAgency() {
            showForAgencyManager();
            $('dl.agency').show();
        }

        function showForOperator() {
            $('dl.operator').show();
        }

        function hideForAgency() {
            hideForAgencyManager();
            $('dl.agency').hide();
        }

        function hideForOperator() {
            $('dl.operator').hide();
        }

        function showForUserManager() {
            $('dl.user-manager').show();
        }

        function hideForUserManager() {
            $('dl.user-manager').hide();
        }

        function showForAgencyManager() {
            $('dl.agency-manager').show();
        }

        function hideForAgencyManager() {
            $('dl.agency-manager').hide();
        }

        function showForCorporationManager() {
            $('dl.corporation-manager').show();
        }

        function hideForCorporationManager() {
            $('dl.corporation-manager').hide();
        }

        function showAgentRateBlock() {
            if (agentRateDefault.next(0).find('img').hasClass('checked')) {
                hideAgentRate();
            } else {
                showAgentRate();
            }
        }

        function showForCorporation() {
            $('dl.corporation').show();
        }

        function hideForCorporation() {
            $('dl.corporation').hide();
        }

        showBlock();

    });
})(jQuery);